import java.util.Arrays;

public class MyStack {
    public int[]elem;
    public int usedSize;
    public MyStack(){
        this.elem=new int[5];
    }

    public void push(int val){
        if(isFull()){
            elem=Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[usedSize]=val;
        usedSize++;
    }
    public boolean isFull(){
        return usedSize==this.elem.length;
    }

    public int pop(){
        if(idEmpty()) {
            throw new RuntimeException("栈为空");
        }
        usedSize--;
        return elem[usedSize];

    }
    public boolean idEmpty(){
        return usedSize==0;
    }

    public int peek(){
        if(idEmpty()) {
            throw new RuntimeException("栈为空");
        }
        return elem[usedSize-1];
    }


    public static void main(String[] args) {
        MyStack stack=new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }
}
